const app = getApp()
import util from "../../utils/util";

Page({
    data: {
        sHeight: null,
        sWidth: null,
        lef: {
            score: 0,
            balls: [],
            skip: 0
        },
        righ: {
            score: 0,
            balls: [],
            skip: 0
        },
        id: null,
        over: 0
    },
    onLoad: function (options) {
        let id = options.id
        console.log("id："+id)
        if (id) {
            this.data.id = id
            wx.request({
                url:util.baseUrl + "/score/" + id,
                method: "GET",
                success:res => {
                    this.data.over = res.data.over
                    this.setData({
                        lef: res.data.lef,
                        righ: res.data.righ
                    })
                }
            })
        } else {
            let score = {}
            score.openid = app.globalData.openid
            score.lef = this.data.lef
            score.righ = this.data.righ
            wx.request({
                url:util.baseUrl + "/score",
                method: "POST",
                data: score,
                success: res => {
                    this.data.id = res.data.id
                }
            })
        }
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        let score = {}
        score.id = this.data.id
        score.openid = app.globalData.openid
        score.lef = this.data.lef
        score.righ = this.data.righ
        score.over = this.data.over
        this.saveScore(score)
        return {
            title: app.globalData.userInfo.nickName + "邀请你计分",
            path:`/pages/score/score?id=`+this.data.id
        }
    },
    showTip(){
        wx.vibrateShort()
        wx.showToast({
            title: '长按篮板将结束比赛',
            icon: 'success',
            duration: 1500
        })
    },
    saveScore(score) {
        wx.request({
            url:util.baseUrl + "/score",
            method: "POST",
            data: score,
            success: res => {
                console.log('save success')
            }
        })
    },
    onShow() {
        this.setData({
            sHeight: app.globalData.screenH,
            sWidth: app.globalData.screenW
        })
    },
    add(e) {
        wx.vibrateShort()
        let ballScore = e.currentTarget.dataset.score
        let ballTeam = e.currentTarget.dataset.team
        // console.log(ballScore + ballTeam)
        let num = parseInt(ballScore)
        this.changeScore(this, num, ballTeam)
    },
    changeScore(that, ballScore, ballTeam) {
        if ( this.data.over ==  1 ) {
            wx.showToast({
                title: '比赛已结束',
                icon: 'fail',
                image: '/src/images/error.png',
                duration: 1000
            })
            return
        }
        let lastScore = that.data[ballTeam].score
        if ((lastScore + ballScore) < 0 || (lastScore + ballScore) >300 ) {
            wx.showToast({
                title: '别闹',
                icon: 'fail',
                image: '/src/images/error.png',
                duration: 1000
            })
            return
        }
        let imgPath = ''
        if ( ballScore >0 ) {
            imgPath = '/src/images/add.png'
            that.data[ballTeam].balls.push(Math.abs(ballScore))
        } else {
            let lastBall = that.data[ballTeam].balls[ that.data[ballTeam].balls.length - 1 ]
            if ( lastBall != Math.abs(ballScore) ) {
                wx.showToast({
                    title: '只能取消上一球',
                    icon: 'fail',
                    image: '/src/images/error.png',
                    duration: 2000
                })
                return;
            }
            imgPath = '/src/images/subtract.png'
            that.data[ballTeam].balls.pop(Math.abs(ballScore))
        }
        let hideFlag = parseInt(that.data[ballTeam].balls.length / 25)
        // console.log(hideFlag)
        that.data[ballTeam].skip = hideFlag * 25

        that.data[ballTeam].score = lastScore+ballScore
        //得分提示
        wx.showToast({
            title: Math.abs(ballScore).toString(),
            icon: 'sucess',
            image: imgPath,
            duration: 300
        })
        that.setData(
            {[ballTeam]: that.data[ballTeam]}
        )
    },
    gameOver(){
        wx.vibrateLong()
        let score = {}
        score.id = this.data.id
        score.openid = app.globalData.openid
        score.lef = this.data.lef
        score.righ = this.data.righ
        this.data.over = 1
        score.over = 1
        this.saveScore(score)
        wx.showToast({
            title: 'O,截图或分享',
            icon: 'success',
            duration: 2000
        })
    }
});